#include "Captuer.h"








int ID_SLIDER = 1;

Captuer::Captuer(const wxString& title)
       : wxFrame(NULL, wxID_ANY, title, wxDefaultPosition, wxSize(350, 200)) {
	
	sampler = new Sampler();
	
	
	cur_width = 75;
	
	wxPanel *panel = new wxPanel(this, wxID_ANY);
	wxPanel *centerPanel = new wxPanel(panel, wxID_ANY);
	
	loadButton = new wxButton(panel, ID_Load, _("Open"));	
	saveButton = new wxButton(panel, ID_Save, _("Save"));
	quitButton = new wxButton(panel, ID_Quit, _("Quit"));
	recordModeButton = new wxToggleButton(panel, ID_RecordMode, _("RECORD MODE"));
	autotriggerButton = new wxToggleButton(panel, ID_AutoTrigger, _("AUTO-TRIGGER"));
	
	
	autotriggerButton->Disable();
	
	
	
	wxBoxSizer *vbox = new wxBoxSizer(wxVERTICAL);
	wxBoxSizer *hbox = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer *hbox2 = new wxBoxSizer(wxHORIZONTAL);
	wxBoxSizer *hbox3 = new wxBoxSizer(wxHORIZONTAL);
	
	m_wid = new VUMeter(panel, wxID_ANY);
	hbox->Add(m_wid, 1, wxEXPAND);
	
	hbox2->Add(loadButton, 1, wxEXPAND);
	hbox2->Add(saveButton, 1, wxEXPAND);
	hbox2->Add(quitButton, 1, wxEXPAND);
	hbox2->Add(recordModeButton, 1, wxEXPAND);
	hbox2->Add(autotriggerButton, 1, wxEXPAND);
	
	
	
	
	
	
	
	centerPanel->SetSizer(hbox3);
	
	vbox->Add(hbox2, 1, wxEXPAND);
	vbox->Add(hbox, 0, wxEXPAND);
	
	panel->SetSizer(vbox);
	
	Centre();
	
	
	wxMenu *menuFile = new wxMenu;
	
	menuFile->Append( ID_About, _T("&About...") );
	menuFile->AppendSeparator();
	menuFile->Append( ID_Load, _T("&Open...") );
	menuFile->Append( ID_Save, _T("&Save...") );
	menuFile->AppendSeparator();
	menuFile->Append( ID_Quit, _T("E&xit") );
	
	wxMenuBar *menuBar = new wxMenuBar;
	menuBar->Append( menuFile, _T("&File") );
	
	SetMenuBar( menuBar );
	
	printf("Finished initing gui\n");
	
	
}


void Captuer::update() {
	if(sampler && m_wid) m_wid->setVolume(sampler->getVolume());
}

Captuer::~Captuer() {
};




BEGIN_EVENT_TABLE(Captuer, wxFrame)

//
// These should be triggered at app level rather than frame level.
// I don't know if they are triggering from here or not?
EVT_KEY_DOWN(Captuer::onKeyDown)
EVT_KEY_UP(Captuer::onKeyUp)


EVT_MENU(ID_Quit, Captuer::OnQuit)
EVT_MENU(ID_Load, Captuer::onLoad)
EVT_MENU(ID_Save, Captuer::onSave)
EVT_BUTTON(ID_Quit, Captuer::OnQuit)

EVT_MENU(ID_About, Captuer::OnAbout)



EVT_BUTTON(ID_Load, Captuer::onLoad)
EVT_BUTTON(ID_Save, Captuer::onSave)
EVT_TOGGLEBUTTON(ID_RecordMode, Captuer::onToggleRecordMode)
EVT_TOGGLEBUTTON(ID_AutoTrigger, Captuer::onToggleAutoTrigger)




END_EVENT_TABLE()














void Captuer::onToggleRecordMode(wxCommandEvent& event) {
	//printf("record mode toggled\n");
	sampler->setRecordMode(recordModeButton->GetValue());
	autotriggerButton->Enable(recordModeButton->GetValue());
}

void Captuer::onToggleAutoTrigger(wxCommandEvent& event) {
	sampler->setAutoTrigger(autotriggerButton->GetValue());
}

void Captuer::onSave(wxCommandEvent& event) {
	// save button
	wxFileDialog *dlg = new wxFileDialog(this, (const wxString)_("Save file"), _(""), _(""), _("DLS files (*.dls)|*.dls|GIG files (*.gig)|*.gig"), wxFD_SAVE|wxFD_OVERWRITE_PROMPT );
	if(dlg->ShowModal()==wxID_OK) {
		if(!sampler->save(std::string(dlg->GetPath().mb_str()))) {
			wxMessageBox(_("There was an error saving the file"),
	_T("Error"), wxOK | wxICON_ERROR, this);
		}
		
		
		
	}

}

void Captuer::onLoad(wxCommandEvent& event) {
	
	wxFileDialog *dlg = new wxFileDialog(this, (const wxString)_("Choose a file"), _(""), _(""), _("DLS and GIG files (*.dls;*.gif)|*.dls;*.gif|DLS files (*.dls)|*.dls|GIG files (*.gig)|*.gig"));
	if(dlg->ShowModal()==wxID_OK) {
		if(!sampler->load(std::string(dlg->GetPath().mb_str()))) {
			wxMessageBox(_T("Invalid file. Perhaps this wasn't created with Captuer."),
	_T("Error"), wxOK | wxICON_ERROR, this);
		}
	}
}

		
int mapQwertyToMidi(char qwertyIn) {
	return qwertyIn - 12;
}

void Captuer::onKeyDown(wxKeyEvent& event) {
	// 32 = space
	// 33 - 126 is keys
	if(event.m_keyCode==32) {// space
		recordModeButton->SetValue(!recordModeButton->GetValue());
		sampler->setRecordMode(recordModeButton->GetValue());
		autotriggerButton->Enable(recordModeButton->GetValue());
	} else {// send to sampler
		sampler->notePressed(mapQwertyToMidi(event.m_keyCode), 100);
	}
}

void Captuer::onKeyUp(wxKeyEvent& event) {

	if(event.m_keyCode!=32) {
		sampler->noteReleased(mapQwertyToMidi(event.m_keyCode));
	}
}




void Captuer::OnQuit(wxCommandEvent& WXUNUSED(event)) {
	Close(TRUE);

// oooh evil, but it will have to do now because the
// above line isn't working in windows yet.
#ifdef __WXMSW__
	exit(0);
#endif
}


void Captuer::OnAbout(wxCommandEvent& WXUNUSED(event)) {
	wxMessageBox(_T("The easy sampler"),
	_T("About Captuer"), wxOK | wxICON_INFORMATION, this);
}








